package com.genymobile.scrcpy.five;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;

import com.genymobile.scrcpy.five.cmd.CmdParser;
import com.genymobile.scrcpy.five.cmd.ContextCmd;
import com.genymobile.scrcpy.five.cmd.ICmd;
import com.genymobile.scrcpy.five.inject.FiveInjector;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * 读取record.sh，并执行里面记录的命令
 */
public class RecordPlayer {
    private final String filename;
    public RecordPlayer(String filename) {
        this.filename = filename;
        Log.d("five666", "=================> do_five_replay is true!!! " + filename);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public void play() {
        List<ICmd> cmdList = new ArrayList<>();
        try (Stream<String> lines = Files.lines(Paths.get(filename))) {
            lines.forEach(line -> {
                // 在这里处理每一行
                ICmd cmd = CmdParser.parse(line);
                if (cmd != null) {
//                        Log.w(FiveInputEventRecorder.class.getSimpleName(), "Cmd: " + cmd);
                    cmdList.add(cmd);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        FiveInjector injector = new FiveInjector();
        for (ICmd cmd : cmdList) {
            Log.i(FiveInputEventRecordServer.class.getSimpleName(), "Running cmd: " + cmd);
            cmd.run(injector);
            Log.i(FiveInputEventRecordServer.class.getSimpleName(), "=====>finished cmd: " + cmd);
        }
    }
}
